package zero.tech.wsnet.net;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import zero.tech.core.ZeroContext;
import zero.tech.core.util.EngineLogUtil;
import zero.tech.wsnet.node.NodeHeartBeatWork;
import zero.tech.wsnet.node.NodeMonitor;
@ConfigurationProperties(prefix = "ws.discovery")
public abstract class WebAppCoreContext implements ApplicationListener<ContextRefreshedEvent> {
	private String server_node_key;
	private String discovery_ip;
	private int discovery_port;
	private static boolean thread = false;

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		if (!thread) {
			thread = true;
			ApplicationContext applicationContext = ZeroContext.applicationContext;
			onApplicationStart(applicationContext);
			try {
				NodeMonitor.createNodeClient(discovery_ip, discovery_port, server_node_key, true);
				new NodeHeartBeatWork().start();
			} catch (Exception e) {
				EngineLogUtil.traceFull(e);
				System.exit(0);
			}
		}
	}

	public String getServer_node_key() {
		return server_node_key;
	}

	public void setServer_node_key(String server_node_key) {
		this.server_node_key = server_node_key;
	}

	public String getDiscovery_ip() {
		return discovery_ip;
	}

	public void setDiscovery_ip(String discovery_ip) {
		this.discovery_ip = discovery_ip;
	}

	public int getDiscovery_port() {
		return discovery_port;
	}

	public void setDiscovery_port(int discovery_port) {
		this.discovery_port = discovery_port;
	}

	public abstract void onApplicationStart(ApplicationContext applicationContext);
}
